﻿var scrollSetp = 500,
    operationWidth = 0,
    leftOperationWidth = 0,
    animatSpeed = 150,
    move = function (selDom) {
        var nav = $("#menu-list");
        var releft = selDom.offset().left - ($(".top_nav").offset().left + 1);
      //  console.log(releft);
      //  console.log($(".top_nav").offset().left + 1)
        var wwidth = parseInt($(".middle_navs").width());
        var left = parseInt(nav.css("margin-left"));
        if (releft < 0 && releft <= wwidth) {
            nav.animate({
                    "margin-left": (left - releft + leftOperationWidth) + "px"
                },
                animatSpeed)
        } else {
            if (releft + selDom.width() > wwidth - operationWidth) {
                nav.animate({
                        "margin-left": (left - releft + wwidth - selDom.width() - operationWidth) + "px"
                    },
                    animatSpeed)
            }
        }
    },
    createmove = function () {
        var nav = $("#menu-list");
        var wwidth = parseInt($(".middle_navs").width());
        var navwidth = parseInt(nav.width());
        if (wwidth - operationWidth < navwidth) {
            nav.animate({
                    "margin-left": "-" + (navwidth - wwidth + operationWidth) + "px"
                },
                animatSpeed)
        }
    },

    closemenu = function () {
        $(this.parentElement).animate({
                "width": "0",
                "padding": "0"
            },
            0,
            function () {
                var jthis = $(this);
                if (jthis.hasClass("active")) {
                    var linext = jthis.next();
                    if (linext.length > 0) {
                        linext.click();
                        move(linext)
                    } else {
                        var liprev = jthis.prev();
                        if (liprev.length > 0) {
                            liprev.click();
                            move(liprev)
                        }
                    }
                }
                this.remove();
                //  $("#page-content .iframe-content[data-url='" + jthis.data("url") + "'][data-value='" + jthis.data("value") + "']").remove()
            });
        event.stopPropagation()
    };

$("#page-prev").bind("click",
    function () {
        var nav = $("#menu-list");
        var left = parseInt(nav.css("margin-left"));
        if (left !== 0) {
            nav.animate({
                    "margin-left": (left + scrollSetp > 0 ? 0 : (left + scrollSetp)) + "px"
                },
                animatSpeed)
        }
    });
$("#page-next").bind("click",
    function () {
        var nav = $("#menu-list");
        var left = parseInt(nav.css("margin-left"));
        var wwidth = parseInt($(".middle_navs").width());
        var navwidth = parseInt(nav.width());
        var allshowleft = -(navwidth - wwidth + operationWidth);
        if (allshowleft !== left && navwidth > wwidth - operationWidth) {
            console.log(allshowleft);
            var temp = (left - scrollSetp);
            nav.animate({
                    "margin-left": (temp < allshowleft ? allshowleft : temp) + "px"
                },
                animatSpeed)
        }
    });
$("#page-operation").bind("click",
    function () {
        var menuall = $("#menu-all");
        if (menuall.is(":visible")) {
            menuall.hide()
        } else {
            menuall.show()
        }
    });
$("body").bind("mousedown",
    function (event) {
        if (!(event.target.id === "menu-all" || event.target.id === "menu-all-ul" || event.target.id === "page-operation" || event.target.id === "page-operation" || event.target.parentElement.id === "menu-all-ul")) {
            $("#menu-all").hide()
        }
    });

$('#menu-list').on('mouseenter', 'li', function() {//绑定鼠标进入事件
    $(this).addClass('hover');
});
$('#menu-list').on('mouseleave', 'li', function() {//绑定鼠标划出事件
    $(this).removeClass('hover');
});


//右边内容大小切换
$('#menu-list').on('dblclick','a[data-toggle="tab"]',function(){
	rightCntViewMax();
})

function rightCntViewMax(){    
	 if (!$('.page-sidebar-closed-hide-logo',parent.document).hasClass('page-sidebar-closed')){ //如果导航没有close就执行close
            $('.page-sidebar-closed-hide-logo',parent.document).addClass('page-sidebar-closed');
            $('.page-sidebar-menu',parent.document).addClass('page-sidebar-menu-closed');
		}
        $('.left_tree').toggleClass('display_none');	//切换tree和右边内容的显示
        $('.right_tbale').toggleClass('marginZero');
	
}

//添加tab栏
function addTab(name,url,this_id){

    $("#page-tab-content .nav-pills .active").removeClass("active");

  /*  if($('#g'+this_id+'').length === 0){
        var strp = this_id.replace("_anchor", "");
        $('.tab-content').append("<div id='g"+this_id+"' data-tuid='"+strp+"' class='tab-pane active' style=\"height:100%;\"><iframe  src='"+url+"' name='ifm' id='ifm'  width='100%' height='100%' scrolling='no' style=\"border:none\"></iframe></div>");
        $('#g'+this_id+'').siblings().removeClass("active");
//        $("#page-tab-content .nav-pills").append(" <li class='active' data-toggle='tooltip' data-placement='bottom' title='"+name+"'><a href='#g"+this_id+"'  data-toggle=\"tab\" role=\"tab\" > <span>"+name+"</span><i class='fa fa-close' data-href='#g"+this_id+"'></i></a></li>");
        $("#page-tab-content .nav-pills").append(" <li class='active' data-toggle='tooltip' data-placement='bottom'><a href='#g"+this_id+"'  data-toggle=\"tab\" role=\"tab\" > <span title='"+name+"'>"+name+"</span><i class='layui-icon layui-close' data-href='#g"+this_id+"'>&#x1006;</i></a></li>");
    }else {
        $("#page-tab-content .nav-pills .active").removeClass("active");
        $("a[ href='#g"+this_id+"']").parent().addClass('active');
        $('#g'+this_id+'').addClass("active").siblings().removeClass("active");
    }*/
    
    
	if($('#g'+this_id+'').length === 0){
		$('.portlet_list').append("<div id='g"+this_id+"' class='tab-pane active' style=\"height:100%;\"><iframe src='"+url+"' width='100%' height='100%' scrolling='no' style=\"border:none\"></iframe></div>");
		$('#g'+this_id+'').siblings().removeClass("active");
	//	$("#page-tab-content .nav-pills").append(" <li class='active' data-toggle='tooltip' data-placement='bottom' title='"+name+"'><a href='#g"+this_id+"'  data-toggle=\"tab\" role=\"tab\" > <span>"+name+"</span><i class='fa fa-close'></i></a></li>");
        $("#page-tab-content .nav-pills").append(" <li class='active' data-toggle='tooltip' data-placement='bottom'><a href='#g"+this_id+"'  data-toggle=\"tab\" role=\"tab\" > <span title='"+name+"'>"+name+"</span><i class='layui-icon layui-close' data-href='#g"+this_id+"'>&#x1006;</i></a></li>");
        createmove();  //添加时移动tab栏
	}else {
		$("#page-tab-content .nav-pills .active").removeClass("active");
		$("a[ href='#g"+this_id+"']").parent().addClass('active');
		$('#g'+this_id+'').addClass("active").siblings().removeClass("active");
		console.log($("a[ href='#g"+this_id+"']").parent().offset().left)
		move($("a[ href='#g"+this_id+"']").parent());
	}

  
    $('.layui-close').unbind('click').click(function(){ //关闭tab栏选项时
        CloseTask($(this))
    });

};

//关闭任务单元时
function CloseTask(currentEle){
    var this_li = currentEle.parent().parent();
    var this_li_prev = currentEle.parent().parent().prev();
    var this_li_next = currentEle.parent().parent().next();
    var portlet_id = currentEle.parent().attr('href').substr(1);
    var portlet_id_obj = $('#'+portlet_id+'');
    var current_cotent = 	portlet_id_obj;
    var current_cotent_prev = portlet_id_obj.prev();
    var current_cotent_next = portlet_id_obj.next();


    if(this_li.hasClass('active')){
        // console.log(this_li.prev().length === 0);
        console.log(this_li.prev());
        if(this_li.prev().length > 0){
            this_li_prev.addClass('active');
            current_cotent_prev.addClass('active');
            move(this_li_prev);
        }else {
        	
        	if(this_li.next().length > 0){
        		this_li_next.addClass('active');
                current_cotent_next.addClass('active');
                move(this_li_next);
        	}
        	
        }
    }

    this_li.remove();
    current_cotent.remove();

}
